//
//  ViewController.m
//  AutoLayoutDemo
//
//  Created by 罗杰 on 16/9/12.
//  Copyright © 2016年 罗杰. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView*v1=[[UIView alloc]init];
    v1.backgroundColor=[UIColor redColor];
//    一定要禁止将Autoresizing Mask转换为约束
    v1.translatesAutoresizingMaskIntoConstraints=NO;
//    要把子view的约束加在父view上
    [self.view addSubview:v1];
    //NSLayoutAttributeLeading就是NSLayoutAttributeLeft NSLayoutAttributeTrailing就是NSLayoutAttributeRight，只是有的国家方向观不同
    NSLayoutConstraint*left=[NSLayoutConstraint constraintWithItem:v1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10];
//    因为iOS中原点在左上角所以使用offset时注意right和bottom用负数(已内边距为参考，往右和网下是越大，所以是负)
    NSLayoutConstraint*right=[NSLayoutConstraint constraintWithItem:v1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10];
    NSLayoutConstraint*top=[NSLayoutConstraint constraintWithItem:v1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:10];
    NSLayoutConstraint*bottom=[NSLayoutConstraint constraintWithItem:v1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10];
    [self.view addConstraints:@[left,right,top,bottom]];
    
    
    
    UIView*sub_v1=[[UIView alloc]init];
    sub_v1.backgroundColor=[UIColor blackColor];
    sub_v1.translatesAutoresizingMaskIntoConstraints=NO;
    [v1 addSubview:sub_v1];
//    在设置宽和高这两个约束时，relatedBy参数使用的是 NSLayoutRelationGreaterThanOrEqual，而不是 NSLayoutRelationEqual。因为 Auto Layout 是相对布局，所以通常你不应该直接设置宽度和高度这种固定不变的值，除非你很确定视图的宽度或高度需要保持不变
    NSLayoutConstraint*centerX_sub_v1=[NSLayoutConstraint constraintWithItem:sub_v1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:v1 attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    NSLayoutConstraint*top_sub_v1=[NSLayoutConstraint constraintWithItem:sub_v1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:v1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:10];
    NSLayoutConstraint*width_sub_v1=[NSLayoutConstraint constraintWithItem:sub_v1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:v1 attribute:NSLayoutAttributeWidth multiplier:0.8 constant:-10];
//    如果是设置view自身的属性，不涉及到与其他view的位置约束关系。比如view自身的宽、高等约束时，方法constraintWithItem:的第四个参数view2（secondItem）应设为
//    nil；且第五个参数attr2（secondAttribute）应设为 NSLayoutAttributeNotAnAttribute 。
    NSLayoutConstraint*height_sub_v1=[NSLayoutConstraint constraintWithItem:sub_v1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:30];
    [v1 addConstraints:@[centerX_sub_v1,top_sub_v1,width_sub_v1,height_sub_v1]];
    
//    四、Auto Layout 关于更新约束的几个方法
//    setNeedsLayout：告知页面需要更新，但是不会立刻开始更新。执行后会立刻调用layoutSubviews。
//    layoutIfNeeded：告知页面布局立刻更新。所以一般都会和setNeedsLayout一起使用。如果希望立刻生成新的frame需要调用此方法，利用这点一般布局动画可以在更新布局后直接使用这个方法让动画生效。
//    layoutSubviews：系统重写布局。
//    setNeedsUpdateConstraints：告知需要更新约束，但是不会立刻开始。
//    updateConstraintsIfNeeded：告知立刻更新约束。
//    updateConstraints：系统更新约束。

    
    
    UIButton*sub_v2=[[UIButton alloc]init];
    sub_v2.translatesAutoresizingMaskIntoConstraints=NO;
    sub_v2.backgroundColor=[UIColor yellowColor];
    [v1 addSubview:sub_v2];
    
    NSLayoutConstraint*leftMargin_sub_v2=[NSLayoutConstraint constraintWithItem:sub_v2 attribute:NSLayoutAttributeLeftMargin relatedBy:NSLayoutRelationEqual toItem:sub_v1 attribute:NSLayoutAttributeLeftMargin multiplier:1.0 constant:0];
    NSLayoutConstraint*top_sub_v2=[NSLayoutConstraint constraintWithItem:sub_v2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:sub_v1 attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10];
    NSLayoutConstraint*height_sub_v2=[NSLayoutConstraint constraintWithItem:sub_v2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:sub_v1 attribute:NSLayoutAttributeHeight multiplier:2.0 constant:20];
    [v1 addConstraints:@[leftMargin_sub_v2,top_sub_v2,height_sub_v2]];
//
//    
    UIView*sub_v3=[[UIView alloc]init];
    sub_v3.backgroundColor=[UIColor  blueColor];
    //一定带上
    sub_v3.translatesAutoresizingMaskIntoConstraints=NO;
    [v1 addSubview:sub_v3];
    NSLayoutConstraint*left_sub_v3=[NSLayoutConstraint constraintWithItem:sub_v3 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:sub_v2 attribute:NSLayoutAttributeRight multiplier:1.0 constant:10];
    NSLayoutConstraint*topMargin_sub_v3=[NSLayoutConstraint constraintWithItem:sub_v3 attribute:NSLayoutAttributeTopMargin relatedBy:NSLayoutRelationEqual toItem:sub_v2 attribute:NSLayoutAttributeTopMargin multiplier:1.0 constant:0];
    NSLayoutConstraint*height_sub_v3=[NSLayoutConstraint constraintWithItem:sub_v3 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:sub_v2 attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    NSLayoutConstraint*rightMargin_sub_v3=[NSLayoutConstraint constraintWithItem:sub_v3 attribute:NSLayoutAttributeRightMargin relatedBy:NSLayoutRelationEqual toItem:sub_v1 attribute:NSLayoutAttributeRightMargin multiplier:1.0 constant:0];
    NSLayoutConstraint*width_sub_v3=[NSLayoutConstraint constraintWithItem:sub_v3 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:sub_v2 attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
    //约束添加到共同的父视图上
    [v1 addConstraints:@[left_sub_v3,topMargin_sub_v3,height_sub_v3,rightMargin_sub_v3,width_sub_v3]];
    
    
    
    UIView*sub_v4=[[UIView alloc]init];
    sub_v4.translatesAutoresizingMaskIntoConstraints=NO;
    sub_v4.backgroundColor=[UIColor greenColor];
    [sub_v2 addSubview:sub_v4];
    NSLayoutConstraint*left_sub_v4=[NSLayoutConstraint constraintWithItem:sub_v4 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:sub_v2 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
    NSLayoutConstraint*height_sub_v4=[NSLayoutConstraint constraintWithItem:sub_v4 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:sub_v4 attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
    NSLayoutConstraint*centerY_sub_v4=[NSLayoutConstraint constraintWithItem:sub_v4 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:sub_v2 attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
    NSLayoutConstraint*right_sub_v4=[NSLayoutConstraint constraintWithItem:sub_v4 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:sub_v2 attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
    [sub_v2 addConstraints:@[left_sub_v4,height_sub_v4,centerY_sub_v4,right_sub_v4]];
    
    
    
    
    UIView*sub_v5=[[UIView alloc]init];
    sub_v5.backgroundColor=[UIColor  orangeColor];
    sub_v5.translatesAutoresizingMaskIntoConstraints=NO;
    [sub_v3 addSubview:sub_v5];
    
    NSLayoutConstraint*right_sub_v5=[NSLayoutConstraint constraintWithItem:sub_v5 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:sub_v3 attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
    NSLayoutConstraint*height_sub_v5=[NSLayoutConstraint constraintWithItem:sub_v5 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:sub_v4 attribute:NSLayoutAttributeWidth multiplier:0.2 constant:0];
    NSLayoutConstraint*centerY_sub_v5=[NSLayoutConstraint constraintWithItem:sub_v5 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:sub_v3 attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
   //约束在最近的俯视图上
    NSLayoutConstraint*left_sub_v5=[NSLayoutConstraint constraintWithItem:sub_v5 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:sub_v4 attribute:NSLayoutAttributeRight multiplier:1.0 constant:30];
    [v1 addConstraints:@[left_sub_v5,right_sub_v5,height_sub_v5,centerY_sub_v5]];



}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
